" VIM Configuration File

set enc=utf-8
set fenc=utf-8
set termencoding=utf-8
set encoding=utf-8

set nocp
filetype plugin on
set nocompatible

let mapleader = ","

set nobackup 
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set backupskip=/tmp/*,/private/tmp/* 
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp 
set writebackup

set backspace=2

" use indentation of previous line
set autoindent
" use intelligent indentation for C
set smartindent
set hidden

" configure tabwidth and insert spaces instead of tabs
set tabstop=4        " tab width is 4 spaces
set shiftwidth=4     " indent also with 4 spaces
"set expandtab        " expand tabs to spaces
set textwidth=90
set ls=2

set t_Co=256

let g:solarized_termcolors=256
"let g:solarized_termtrans = 1
colorscheme solarized
set background=dark

syntax on

" turn line numbers on
set number
set cursorline
 
" highlight matching braces
set showmatch

"Con space tambien entro a modo edicion
map <space> i

if &term =~ '^screen'
	" tmux will send xterm-style keys when its xterm-keys option is on
	execute "set <xUp>=\e[1;*A"
	execute "set <xDown>=\e[1;*B"
	execute "set <xRight>=\e[1;*C"
	execute "set <xLeft>=\e[1;*D"
endif

" Activar numeros de lineas
map <F1> :set nonumber!<CR>
imap <F1><ESC> :set nonumber!<CR>i

" Autocompletado
imap <C-k> <C-x><C-p>

" Guardar el archivo
nmap  <F2> :w<CR>
imap <F2> <ESC>:w<CR>i

" Atajos para los tags
map <F5> :!ctags -R --c++-kinds=+p --perl-kinds=+cls --fields=+iaS --extra=+q --exclude=.svn . <CR>

map <F6> :InspectClass<CR>

" Atajo para cambiar de directorio
nnoremap ,cd :cd %:p:h<CR>:pwd<CR>

" Atajos para moverse entre las ventanas y los buffers
map <C-Right> <C-W><Right>
map <C-Left> <C-W><Left>
map <C-Down> <C-W><Down>
map <C-Up> <C-W><Up>

" Grep
nnoremap <silent> <F3> :Rgrep<CR>
let Grep_Skip_Files = '*.bak *~ *svn* tags *.tmp'

"Paste
map <F9> :set invpaste<CR>
imap <F9> <C-O>:set invpaste<CR>
set pastetoggle=<F9>


" Autocompletado
let g:acp_behaviorKeywordLength = 4

" NERDTree
let g:NERDTreeWinSize=60
let g:NERDTreeQuitOnOpen=1
let g:NERDTreeShowBookmarks=1
let NERDTreeIgnore=['\.vim$', '\~$', '.o$', 'tags$']
map <F4> :NERDTree<CR>

" Comentar en modo visual
vmap f ,ci

nnoremap <silent> <F8> :TlistToggle<CR>
let Tlist_Close_On_Select = 1
let Tlist_Show_One_File = 1
let Tlist_Use_Right_Window = 1
let Tlist_WinWidth = 60
let Tlist_GainFocus_On_ToggleOpen = 1
let Tlist_Close_On_Select = 1
let Tlist_Exit_OnlyWindow = 1

"Yanktmp
map ,y :call YanktmpYank()<CR>
map ,p :call YanktmpPaste_p()<CR>
map ,P :call YanktmpPaste_P()<CR>

"RemoteYank
vmap <leader>R <esc>:'<,'>:w !remote_yank.sh<CR>

let g:ctrlp_max_height = 30
let g:ctrlp_working_path_mode = 0
let g:ctrlp_extensions = ['tag', 'buffertag', 'line']

nnoremap <silent> sj     :CtrlPBuffer<CR>
nnoremap <silent> sk     :CtrlPCurFile<CR>
nnoremap <silent> sl     :CtrlP<CR> 

nnoremap <silent> sn     :CtrlPMRUFiles<CR> 

nnoremap <silent> sm     :CtrlPBufTag<CR>
nnoremap <silent> s,     :CtrlPBufTagAll<CR>
nnoremap <silent> s.     :CtrlPTag<CR>

nnoremap <silent> s/     :CtrlPLine<CR>


" Reload snippets
function! ReloadSnippets( snippets_dir, ft )
	if strlen( a:ft ) == 0
	let filetype = "_"
	else
	let filetype = a:ft
	endif

	call ResetSnippets()
call GetSnippets( a:snippets_dir, filetype )
	endfunction

nmap ,rr :call ReloadSnippets(snippets_dir, &filetype)<CR>

" Perltidy
let g:perlTidyOptions='-pbp -et=4 -ce -l=90 -pt=2'

" Uncrustify
"

" Perl syntax check
map <leader>s :call Perl_SyntaxCheck()<CR>
function! Perl_SyntaxCheck ()

	exe ":cclose"
	let l:currentbuffer   = bufname("%")
	let l:fullname        = expand("%:p")
	silent exe  ":update"

	exe ':set makeprg=perl\ -c\ -I/epl/src/devel/perl\ -I/epl/src/perl/perl\ -I/epl/src/admin/perl\ -I/epl/src/devel/perl\ -I/epl/src/admin/tests/admin'
	exe ':setlocal errorformat=
		\%-G%.%#had\ compilation\ errors.,
		\%-G%.%#syntax\ OK,
		\%m\ at\ %f\ line\ %l.,
		\%+A%.%#\ at\ %f\ line\ %l\\,%.%#,
		\%+C%.%#'
	let	l:fullname	= fnameescape( l:fullname )
	silent exe  ':make  '.l:fullname

	exe ":botright cwindow" 
  	exe ':setlocal errorformat='
    exe "set makeprg=make"

	redraw!
	if l:currentbuffer ==  bufname("%")
		echohl Search
		echomsg l:currentbuffer." : Syntax is OK"
		echohl None
		return 0
	else
		setlocal wrap
		setlocal linebreak
	endif
endfunction


" Perl Critic check
map <leader>c :call Perl_Critic()<CR>
function! Perl_Critic ()

	exe ":cclose"
	let l:currentbuffer   = bufname("%")
	let l:fullname        = expand("%:p")
	silent exe  ":update"

	exe ':set makeprg=perlcritic\ --exclude\ ProhibitSubroutinePrototypes\ --stern\ --exclude\ ProhibitExplicitReturnUndef\ --noprofile\ --exclude\ RequireArgUnpacking\ --exclude\ RequireFinalReturn\ --exclude\ RequireUseWarnings\ --exclude\ ProhibitBuiltinHomonyms\ --exclude\ ProhibitStringyEval\ --exclude\ RequireBriefOpen\ --verbose\ 1'
	exe ':setlocal errorformat=%f:%l:%c:%m'
	let	l:fullname	= fnameescape( l:fullname )
	silent exe  ':make  '.l:fullname

	exe ":botright cwindow" 
  	exe ':setlocal errorformat='
    exe "set makeprg=make"

	redraw!
	if l:currentbuffer ==  bufname("%")
		echohl Search
		echomsg l:currentbuffer." : Code is OK"
		echohl None
		return 0
	else
		setlocal wrap
		setlocal linebreak
	endif
endfunction
